/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"
#include "TestSession.h"
#include "TestRoom.h"

#include "MudConsumable.h"
#include "MudPlayer.h"
#include "color.h"
#include "Global.h"
#include "Effect.h"
#include "Parser.h"
#include "ParserFactory.h"

using namespace std;
using namespace Chronos;

MudPlayer* global_player = 0;
int testDrinkEffectCalled = 0;
int testFoodEffectCalled = 0;

void testDrinkEffect(MudPlayer& player) {
    global_player = &player;
    testDrinkEffectCalled++;
}

void testFoodEffect(MudPlayer& player) {
    global_player = &player;
    testFoodEffectCalled++;
}


class MudConsumableTest { 
public:
	MudConsumableTest()
        : parser(*ParserFactory::create())
		, session("player")
		, player(session.getPlayer())
		, room("room")
        , drink(new MudDrink(0, "beer", "", 1, testDrinkEffect))
        , food(new MudFood(0, "hamburger", "", 1, testFoodEffect))
	{
		player.addItem(drink);
		player.addItem(food);
		room.addPlayer(player, "");
		session.clear();
        global_player = 0;
        testDrinkEffectCalled = 0;
        testFoodEffectCalled = 0;
	}
	~MudConsumableTest() {
        room.removePlayer(player, "");
        delete &parser;
	}

    Parser& parser;
	TestSession session;
	MudPlayer& player;
	TestRoom room;
    MudDrink* drink;
    MudFood* food;
};


TEST(MudConsumableTest, testDrink) {
    parser.parse(session, "drink nonexist");
	session.SESSION_CHECKWRITTEN("You don't have a nonexist.\n");
	room.ROOM_CHECKWRITTEN("");

    parser.parse(session, "drink beer");
	session.SESSION_CHECKWRITTEN("You drink the beer.\n");
	room.ROOM_CHECKWRITTEN("player drinks a beer.\n");
    ASSERT_EQUAL(&player, global_player);
    ASSERT_EQUAL(1, testDrinkEffectCalled);
}


TEST(MudConsumableTest, testFood) {
    parser.parse(session, "eat nonexist");
	session.SESSION_CHECKWRITTEN("You don't have a nonexist.\n");
	room.ROOM_CHECKWRITTEN("");

    parser.parse(session, "eat hamburger");
	session.SESSION_CHECKWRITTEN("You eat the hamburger.\n");
	room.ROOM_CHECKWRITTEN("player eats a hamburger.\n");
    ASSERT_EQUAL(&player, global_player);
    ASSERT_EQUAL(1, testFoodEffectCalled);
}


TEST(MudConsumableTest, testDrinkingFood) {
    parser.parse(session, "drink hamburger");
	session.SESSION_CHECKWRITTEN("You cannot drink that.\n");
	room.ROOM_CHECKWRITTEN("");
    ASSERT_EQUAL(0, testDrinkEffectCalled);
    ASSERT_EQUAL(0, testFoodEffectCalled);
}


TEST(MudConsumableTest, testEatingDrink) {
    parser.parse(session, "eat beer");
	session.SESSION_CHECKWRITTEN("You cannot eat that.\n");
	room.ROOM_CHECKWRITTEN("");
    ASSERT_EQUAL(0, testDrinkEffectCalled);
    ASSERT_EQUAL(0, testFoodEffectCalled);
}

